package com.handpay.zztong.hp;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.handpay.framework.utils.CommonUtils;
import com.handpay.framework.utils.DateUtils;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.bean.ActionBar;
import com.handpay.zztong.hp.bean.PurchaseStatus;
import com.handpay.zztong.hp.bean.PurchaseStatus.PurchaseEvent;
import com.handpay.zztong.hp.log.HPLog;

@SuppressLint("NewApi")
public class ApplyPurchaseResult extends ZZTongActivity {
	public TextView TPurchaseAmount,purchaseStatustv,purchaseTime,failReason;
	private ImageView purchaseTimeline,failReasonLine;
	private LinearLayout purchaseTimell,failReasonll;
	private PurchaseStatus purchaseList  = null ;
	private PurchaseEvent event_b= null,event_a= null,event_c = null;
	private TextView aptime_a,aptime_b,aptime_c,apstatus_a,apstatus_b,apstatus_c;
	private ImageView precess_a,precess_b,precess_c,connect_a,connect_b;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		setContentView(R.layout.purchase_applyresult);
		super.onCreate(savedInstanceState);
		
		if(getIntent() != null){
			purchaseList = (PurchaseStatus)getIntent().getSerializableExtra("purchaseStatus");
		}
		initView();
	}
	/**
	 * 初始化控件
	 */
	public void initView(){
		//初始化控件
//		findViewById(R.id.left_bar).setOnClickListener(this);
//		((TextView)findViewById(R.id.title)).setText(this.getString(R.string.purchaseResult));
		TPurchaseAmount = (TextView)findViewById(R.id.applyPurchaseAmount);
		HPLog.i("purchaseList.getAmount()", "：purchaseList.getAmount()："+ purchaseList.getAmount());
		TPurchaseAmount.setText(CommonUtils.formatMoneyDouble(Double.parseDouble(purchaseList.getAmount()+"")/100)+"元");
		purchaseStatustv = (TextView)findViewById(R.id.purchaseStuts);
		
		purchaseTimeline = (ImageView)findViewById(R.id.purchaseTimeline);
		purchaseTimell = (LinearLayout)findViewById(R.id.purchaseTimell);
		purchaseTime = (TextView)findViewById(R.id.purchaseTime);
		failReasonLine = (ImageView)findViewById(R.id.fail_reason_line);
		failReasonll = (LinearLayout)findViewById(R.id.fail_reason);
		failReason = (TextView)findViewById(R.id.failureReason); 
		aptime_a = (TextView)findViewById(R.id.aptime_a);
		aptime_b = (TextView)findViewById(R.id.aptime_b);
		aptime_c = (TextView)findViewById(R.id.aptime_c);
		apstatus_a = (TextView)findViewById(R.id.apstatus_a);
		apstatus_b = (TextView)findViewById(R.id.apstatus_b);
		apstatus_c = (TextView)findViewById(R.id.apstatus_c);
		precess_a = (ImageView)findViewById(R.id.precess_a);
		precess_b = (ImageView)findViewById(R.id.precess_b);
		precess_c = (ImageView)findViewById(R.id.precess_c);
		connect_a = (ImageView)findViewById(R.id.connect_a);
		connect_b = (ImageView)findViewById(R.id.connect_b);
		//purchaseList.getStatus();
		if("WW".equals(purchaseList.getStatus())){
			purchaseTimeline.setVisibility(View.GONE);
			purchaseTimell.setVisibility(View.GONE);
			failReasonLine.setVisibility(View.GONE);
			failReasonll.setVisibility(View.GONE);
			purchaseStatustv.setText(purchaseList.getStatusDesc()); 
			event_a = purchaseList.getPurchaseList().get(0);
			event_b = purchaseList.getPurchaseList().get(1);
			event_c = purchaseList.getPurchaseList().get(2);
			HPLog.e("yangjj------------------>>>>>>>>>>>>>>>>>>>>", "event_a.getEventTime():" + event_a.getEventTime());
			
			aptime_a.setText(DateUtils.formatDateToString(DateUtils.formatStringToDate(event_a.getEventTime(), "yyyyMMddHHmmss"), "yyyy-MM-dd HH:mm:ss"));
			apstatus_a.setText(event_a.getEventDesc());
			aptime_b.setText(DateUtils.formatDateToString(DateUtils.formatStringToDate(event_b.getEventTime().substring(0, 8), "yyyyMMdd"), "yyyy-MM-dd"));
			apstatus_b.setText(event_b.getEventDesc());
			aptime_c.setText(DateUtils.formatDateToString(DateUtils.formatStringToDate(event_c.getEventTime().substring(0, 8), "yyyyMMdd"), "yyyy-MM-dd"));
			apstatus_c.setText(event_c.getEventDesc());
			if("1".equals(event_a.getIsFinish()) && "0".equals(event_b.getIsFinish()) &&"0".equals(event_c.getIsFinish())){ 
				precess_a.setImageDrawable(getResources().getDrawable(R.drawable.processahover));
				connect_a.setImageDrawable(getResources().getDrawable(R.drawable.connectb));
				precess_b.setImageDrawable(getResources().getDrawable(R.drawable.processa));
				connect_b.setImageDrawable(getResources().getDrawable(R.drawable.connecta));
				precess_c.setImageDrawable(getResources().getDrawable(R.drawable.processa));
			}
			
			if("1".equals(event_a.getIsFinish()) && "1".equals(event_b.getIsFinish()) &&"0".equals(event_c.getIsFinish())){ 
				precess_a.setImageDrawable(getResources().getDrawable(R.drawable.processahover));
				connect_a.setImageDrawable(getResources().getDrawable(R.drawable.connectc));
				precess_b.setImageDrawable(getResources().getDrawable(R.drawable.processahover));
				connect_b.setImageDrawable(getResources().getDrawable(R.drawable.connectb));
				precess_c.setImageDrawable(getResources().getDrawable(R.drawable.processa));
			}
			if("1".equals(event_a.getIsFinish()) && "1".equals(event_b.getIsFinish()) &&"1".equals(event_c.getIsFinish())){ 
				precess_a.setImageDrawable(getResources().getDrawable(R.drawable.processahover));
				connect_a.setImageDrawable(getResources().getDrawable(R.drawable.connectc));
				precess_b.setImageDrawable(getResources().getDrawable(R.drawable.processahover));
				connect_b.setImageDrawable(getResources().getDrawable(R.drawable.connectc));
				precess_c.setImageDrawable(getResources().getDrawable(R.drawable.processahover));
			}
			
		} else if("W1".equals(purchaseList.getStatus())){
			
			findViewById(R.id.purchase_success_status).setVisibility(View.GONE);
			purchaseTime.setText(DateUtils.formatDateToString(DateUtils.formatStringToDate(purchaseList.getRequestTime(), "yyyyMMddHHmmss"), "yyyy-MM-dd HH:mm:ss"));			
			((TextView)findViewById(R.id.purchaseTime_tv)).setText(getResources().getString(R.string.applyTime_tip));
			purchaseStatustv.setText(purchaseList.getStatusDesc());
			purchaseStatustv.setTextColor(Color.RED);
			
			if(!TextUtils.isEmpty(purchaseList.getMessage())){
				failReason.setText(purchaseList.getMessage());
			}else {
				failReason.setText(purchaseList.getStatusDesc());
			}
			
		} else if("01".equals(purchaseList.getStatus())){
			
			findViewById(R.id.purchase_success_status).setVisibility(View.GONE);
			((TextView)findViewById(R.id.purchaseTime_tv)).setText(getResources().getString(R.string.applyTime_tip));
			purchaseTime.setText(DateUtils.formatDateToString(DateUtils.formatStringToDate(purchaseList.getRequestTime(), "yyyyMMddHHmmss"), "yyyy-MM-dd HH:mm:ss"));//在这里设置申请时间
			purchaseStatustv.setText(purchaseList.getStatusDesc());
			((TextView)findViewById(R.id.failure_tv)).setText(getResources().getString(R.string.purchaseTime)); //purchaseTime
			failReason.setText(DateUtils.formatDateToString(DateUtils.formatStringToDate(purchaseList.getPurchaseTime(), "yyyyMMddHHmmss"), "yyyy-MM-dd HH:mm:ss"));
		
		} else if("02".equals(purchaseList.getStatus())){
				
			findViewById(R.id.purchase_success_status).setVisibility(View.GONE);
			purchaseStatustv.setText(purchaseList.getStatusDesc());
			purchaseStatustv.setTextColor(Color.RED);
			findViewById(R.id.applyTimeline).setVisibility(View.VISIBLE);
			findViewById(R.id.applyTimell).setVisibility(View.VISIBLE);
			((TextView)findViewById(R.id.applyTime)).setText(DateUtils.formatDateToString(DateUtils.formatStringToDate(purchaseList.getRequestTime(), "yyyyMMddHHmmss"), "yyyy-MM-dd HH:mm:ss"));
			if(!TextUtils.isEmpty(purchaseList.getPurchaseTime())){
				purchaseTime.setText(DateUtils.formatDateToString(DateUtils.formatStringToDate(purchaseList.getPurchaseTime(), "yyyyMMddHHmmss"), "yyyy-MM-dd HH:mm:ss"));
			}else {
				purchaseTimeline.setVisibility(View.GONE);
				purchaseTimell.setVisibility(View.GONE);
			}
			
			if(!TextUtils.isEmpty(purchaseList.getMessage())){
				failReason.setText(purchaseList.getMessage());
			}else {
				failReason.setText(purchaseList.getStatusDesc());
			}
		
		}else if("00".equals(purchaseList.getStatus())){
			
			event_a = purchaseList.getPurchaseList().get(0);
			event_b = purchaseList.getPurchaseList().get(1);
			event_c = purchaseList.getPurchaseList().get(2);
			purchaseTimeline.setVisibility(View.GONE);
			purchaseTimell.setVisibility(View.GONE);
			failReasonLine.setVisibility(View.GONE);
			failReasonll.setVisibility(View.GONE);
			purchaseStatustv.setText(purchaseList.getStatusDesc()); 
			aptime_a.setText(DateUtils.formatDateToString(DateUtils.formatStringToDate(event_a.getEventTime(), "yyyyMMddHHmmss"), "yyyy-MM-dd HH:mm:ss"));
			apstatus_a.setText(event_a.getEventDesc());
			aptime_b.setText(DateUtils.formatDateToString(DateUtils.formatStringToDate(event_b.getEventTime(), "yyyyMMddHHmmss"), "yyyy-MM-dd HH:mm:ss"));
			apstatus_b.setText(event_b.getEventDesc());
			aptime_c.setText(DateUtils.formatDateToString(DateUtils.formatStringToDate(event_c.getEventTime().substring(0,8), "yyyyMMdd"), "yyyy-MM-dd"));
			apstatus_c.setText(event_c.getEventDesc());
			if("1".equals(event_a.getIsFinish()) && "1".equals(event_b.getIsFinish()) &&"1".equals(event_c.getIsFinish())){ 
				precess_a.setImageDrawable(getResources().getDrawable(R.drawable.processahover));
				connect_a.setImageDrawable(getResources().getDrawable(R.drawable.connectc));
				precess_b.setImageDrawable(getResources().getDrawable(R.drawable.processahover));
				connect_b.setImageDrawable(getResources().getDrawable(R.drawable.connectc));
				precess_c.setImageDrawable(getResources().getDrawable(R.drawable.processahover));
			}
			if("1".equals(event_a.getIsFinish()) && "1".equals(event_b.getIsFinish()) &&"0".equals(event_c.getIsFinish())){
				precess_a.setImageDrawable(getResources().getDrawable(R.drawable.processahover));
				connect_a.setImageDrawable(getResources().getDrawable(R.drawable.connectc));
				precess_b.setImageDrawable(getResources().getDrawable(R.drawable.processahover));
				connect_b.setImageDrawable(getResources().getDrawable(R.drawable.connectb));
				precess_c.setImageDrawable(getResources().getDrawable(R.drawable.processa));
			}
		}

			
	}
	
	@Override
	protected boolean isShownTopBar() {
		return true;
	}

	@Override
	protected boolean isShownTabs() {
		return false;
	}
	
//	@Override
//	public void onLeftBar(View v) {
//		setResult(REQUEST_APPLYPR_CODE);
//		goBack();
//	}
	@Override
	public ActionBar getActivityActionBar() {
		setResult(REQUEST_APPLYPR_CODE);
		return new ActionBar(this.getString(R.string.purchaseResult), true);
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK){
			setResult(REQUEST_APPLYPR_CODE);
		}
		return super.onKeyDown(keyCode, event);
	}

}
